import { PerspectiveCamera, WebGLRenderer } from "three";
class Resizer {
  #container; // 容器
  #camera; // 相机
  #renderer; // 渲染器
  onResize = () => {}; // 页面大小变化

  /**
   * @param {Element} container - 容器
   * @param {PerspectiveCamera} camera - 相机
   * @param {WebGLRenderer} renderer - 渲染器
   */
  constructor(container, camera, renderer) {
    this.#container = container;
    this.#camera = camera;
    this.#renderer = renderer;
    // 初始化size
    this.#setSize();
    // 监听页面大小变化
    const resizeObserver = new ResizeObserver(() => {
      this.#setSize();
      this.onResize();
    });
    resizeObserver.observe(container);
  }

  #setSize() {
    this.#camera.aspect =
      this.#container.clientWidth / this.#container.clientHeight;
    this.#camera.updateProjectionMatrix();
    this.#renderer.setSize(
      this.#container.clientWidth,
      this.#container.clientHeight
    );
    this.#renderer.setPixelRatio(window.devicePixelRatio);
  }
}
export { Resizer };
